﻿@model NCP.Recruiting.WebApp.Models.JobPositionListViewModel
@using NCP.Recruiting.WebApp.Models;
@using NCP.Recruiting.ApplicationServices.Shared;
@using NCP.Recruiting.WebApp.Extensions;
@{
    ViewBag.Title = "浏览职位";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script type="text/javascript">
    var defaultValue = '@ViewBag.DefaultKeyword';

    $(document).ready(function () {
        
        @if(ViewBag.PostError == true )
        {
            @:alert('剩余发布个数不足，无法发布');
        }

        //表格中编辑菜单下拉框		
        $('.manage').hover(function () {
            $(this).addClass('on');
            $(this).parent().find('.plist').show();
            $(this).parent().siblings().find('.plist').hide();
        }, function () {
            $(this).removeClass('on');
            $(this).parent().find('.plist').hide();
            $(this).parent().siblings().find('.plist').hide();
        });

        $('.plist').hover(function () {
            $(this).show();
            $(this).parent().find('.manage').addClass('on');
        }, function () {
            $(this).hide();
            $(this).parent().find('.manage').removeClass('on');
        });

        //表格隔行变色，最后一行没有边框
        $('.wrapContents table tbody tr:last-child td').css({ 'border-bottom': 'none' });
        $('.wrapContents table tbody tr:even').css({ 'background': '#f0f0f0' });
        $('.wrapContents table tbody tr:even').css({ 'background': '#fbfbfb' });

        //关键字输入框失去焦点
        $('#txtKeyword').bind('blur', function () {
            if (this.value == '') {
                this.value = defaultValue;
                this.className = "search_input";
            }
        });

        //关键字输入框获取焦点
        $('#txtKeyword').bind('focus', function () {
            if (this.value == defaultValue) {
                this.value = '';
            }
            this.className = "search_input_haveValue";
        });

        //过滤按钮点击事件
        $('#btnSearch').bind('click', function () {
            if ($('#txtKeyword').val() != defaultValue) {
                location.href = '@Url.Action("List")' + '?keyword=' + $('#txtKeyword').val() + '&jobQueryStatus=@(Model.JobQueryStatus)';
            }
            else {
                alert('请输入关键字！');
            }
        });

        //初始化关键字输入框
        iniSearchTextBox(defaultValue);

        $('.chkAll').bind('click', function () {
            $('.itemChecked').prop('checked', $(this).prop('checked'));
        });

        //操作按钮点击事件
        $('.action').bind('click', function () {
            var label = $(this).val();
            if (!confirm("确定要" + label + "职位吗？")) {
                return false;
            }
        });

        //批处理操作按钮点击事件
        $('.batchAction').bind('click', function () {
            var selectedCount = $('.itemChecked:checked').length;
            if (selectedCount == 0) {
                alert("请先选择要进行操作的职位！");
                return false;
            }
            else {
                var label = $(this).val();
                if (!confirm("确定要" + label + "职位吗？")) {
                    return false;
                }
            }

            if(selectedCount > @(Model.CompanyInfo.RemainingQuantity)){
                alert("职位发布个数只剩@(Model.CompanyInfo.RemainingQuantity)个，无法全部发布，请重新选择");
            }
        });

        $('.restore').bind('click', function () {
            $('#hidSubmitId').val($(this).parent().find('#hidId').val());
            $('#btnRestore').click();
        });

        //设置弹出菜单
        $('.select').button({
            text: false,
            //text: "管理",
            icons: {
                primary: "ui-icon-triangle-1-s"
            }
        }).click(function () {
            //设置菜单状态
            var status = $(this).parent().find('#hidStatus').val();

            var lbtnRefresh = $('#lbtnRefresh');
            var lbtnPause = $('#lbtnPause');
            var lbtnPost = $('#lbtnPost');
            var lbtnPreview = $('#lbtnPreview');
            
            //发布
            if (status == '@((int)JobPositionStatus.Active)') {
                lbtnRefresh.show();
                lbtnPause.show();
                lbtnPost.hide();
            }
            else {
                lbtnRefresh.hide();
                lbtnPause.hide();
                lbtnPost.show();
                
                //如果没有剩余个数，不允许发布
                if(@(Model.CompanyInfo.RemainingQuantity) <= 0)
                {
                    lbtnPost.attr("disabled", "disabled");
                }
            }

            $('#hidSubmitId').val($(this).parent().find('#hidId').val());

            lbtnPreview.unbind('click');
            lbtnPreview.bind('click',function(){
                alert($('#hidSubmitId').val());
            })

            var menu = $('#menuItem').show().position({
                my: "right top",
                at: "right bottom",
                of: this
            });

            $(document).one("click", function () {
                menu.hide();
            });

            return false;
        });

        $('#menuItem').hide().menu();

        //设置排序号输入格式
        $('.sortOrder').numeral();

        //初始化控件状态
        iniControlDisplayState();

        //排序变更
        $('.sortOrder').bind('blur', function () {
            //如果文本框中的值修改过了，则用AJAX提交变更情况
            if ($(this).val() != $(this).attr('oldValue')) {
                var id = $(this).attr('idValue');
                $.ajax(
                {
                    url: '/JobPosition/ChangeSortOrder?id=' + id + "&sortOrder=" + $(this).val(),
                    type: "POST",
                    success: $.proxy(function () {
                        $(this).attr('oldValue', $(this).val());
                    }, this)
                });
            }
        });
    });

    function iniSearchTextBox(defaultValue) {
        if ($('#txtKeyword').val() == "") {
            $('#txtKeyword').val(defaultValue);
        }
        $('#txtKeyword').toggleClass("search_input", $('#txtKeyword').val() == defaultValue);
        $('#txtKeyword').toggleClass("search_input_haveValue", $('#txtKeyword').val() != defaultValue);
    }

    //初始化列显示状态
    function iniControlDisplayState() {
        //数据列
        var sortOrderIndex = $('#colSortOrder').index();
        var sortOrderInputIndex = $('#colSortOrderInput').index();
        var dateCreatedIndex = $('#colDateCreated').index();
        var startDateIndex = $('#colStartDate').index();
        var repostedIndex = $('#colReposted').index();
        var endDateIndex = $('#colEndDate').index();
        var dateDeletedIndex = $('#colDateDeleted').index();
        //批量操作按钮
        var btnPauseSelected = $('#btnPauseSelected').button().hide();
        var btnRefreshSelected = $('#btnRefreshSelected').button().hide();
        var btnPostSelected = $('#btnPostSelected').button().hide();
        var btnRestoreSelected = $('#btnRestoreSelected').button().hide();

        var btnDeleteSelected = $('#btnDeleteSelected').button();

        var dataRows = $('#tblJobList tr');

        if ('@(Model.JobQueryStatus == JobQueryStatus.All)' == 'True') {
            dataRows.find('th:eq(' + sortOrderInputIndex + '),td:eq(' + sortOrderInputIndex + ')').hide();
            dataRows.find('th:eq(' + dateCreatedIndex + '),td:eq(' + dateCreatedIndex + ')').hide();
            dataRows.find('th:eq(' + dateDeletedIndex + '),td:eq(' + dateDeletedIndex + ')').hide();
        }
        else if ('@(Model.JobQueryStatus == JobQueryStatus.Active)' == 'True') {
            dataRows.find('th:eq(' + sortOrderIndex + '),td:eq(' + sortOrderIndex + ')').hide();
            dataRows.find('th:eq(' + dateCreatedIndex + '),td:eq(' + dateCreatedIndex + ')').hide();
            dataRows.find('th:eq(' + dateDeletedIndex + '),td:eq(' + dateDeletedIndex + ')').hide();

            btnPauseSelected.show();
            btnRefreshSelected.show();
        }
        else if ('@(Model.JobQueryStatus == JobQueryStatus.New)' == 'True') {
            dataRows.find('th:eq(' + sortOrderInputIndex + '),td:eq(' + sortOrderInputIndex + ')').hide();
            dataRows.find('th:eq(' + sortOrderIndex + '),td:eq(' + sortOrderIndex + ')').hide();
            dataRows.find('th:eq(' + startDateIndex + '),td:eq(' + startDateIndex + ')').hide();
            dataRows.find('th:eq(' + repostedIndex + '),td:eq(' + repostedIndex + ')').hide();
            dataRows.find('th:eq(' + endDateIndex + '),td:eq(' + endDateIndex + ')').hide();
            dataRows.find('th:eq(' + dateDeletedIndex + '),td:eq(' + dateDeletedIndex + ')').hide();

            btnPostSelected.show();
        }
        else if ('@(Model.JobQueryStatus == JobQueryStatus.Pause)' == 'True' || '@(Model.JobQueryStatus == JobQueryStatus.Closed)' == 'True') {
            dataRows.find('th:eq(' + sortOrderInputIndex + '),td:eq(' + sortOrderInputIndex + ')').hide();
            dataRows.find('th:eq(' + sortOrderIndex + '),td:eq(' + sortOrderIndex + ')').hide();
            dataRows.find('th:eq(' + dateCreatedIndex + '),td:eq(' + dateCreatedIndex + ')').hide();
            dataRows.find('th:eq(' + dateDeletedIndex + '),td:eq(' + dateDeletedIndex + ')').hide();

            btnPostSelected.show();
        }
        else if ('@(Model.JobQueryStatus == JobQueryStatus.Deleted)' == 'True') {
            dataRows.find('th:eq(' + sortOrderInputIndex + '),td:eq(' + sortOrderInputIndex + ')').hide();
            dataRows.find('th:eq(' + sortOrderIndex + '),td:eq(' + sortOrderIndex + ')').hide();
            dataRows.find('th:eq(' + dateCreatedIndex + '),td:eq(' + dateCreatedIndex + ')').hide();
            dataRows.find('th:eq(' + startDateIndex + '),td:eq(' + startDateIndex + ')').hide();
            dataRows.find('th:eq(' + repostedIndex + '),td:eq(' + repostedIndex + ')').hide();
            dataRows.find('th:eq(' + endDateIndex + '),td:eq(' + endDateIndex + ')').hide();

            btnRestoreSelected.show();
        }

        if(@(Model.CompanyInfo.RemainingQuantity) <= 0){
            btnPostSelected.attr('disabled','disabled');
        }
    };
</script>
<div id="wrap">
    <div class="wrapContents">
        @using (Html.BeginForm())
        {             
            <h1>
                浏览职位</h1>
            <div class="positions">
                @Html.ActionLink("所有职位", "List", new { jobQueryStatus = JobQueryStatus.All }, new { @class = Model.JobQueryStatus == JobQueryStatus.All ? "cur" : "none" })
                @Html.ActionLink("发布中的职位", "List", new { jobQueryStatus = JobQueryStatus.Active }, new { @class = Model.JobQueryStatus == JobQueryStatus.Active ? "cur" : "none" })
                @Html.ActionLink("新建的职位", "List", new { jobQueryStatus = JobQueryStatus.New }, new { @class = Model.JobQueryStatus == JobQueryStatus.New ? "cur" : "none" })
                @Html.ActionLink("暂停的职位", "List", new { jobQueryStatus = JobQueryStatus.Pause }, new { @class = Model.JobQueryStatus == JobQueryStatus.Pause ? "cur" : "none" })
                @Html.ActionLink("过期的职位", "List", new { jobQueryStatus = JobQueryStatus.Closed }, new { @class = Model.JobQueryStatus == JobQueryStatus.Closed ? "cur" : "none" })
                @Html.ActionLink("已删除的职位", "List", new { jobQueryStatus = JobQueryStatus.Deleted }, new { @class = Model.JobQueryStatus == JobQueryStatus.Deleted ? "cur" : "none" })
            </div>
            <div class="search">
                <div class="search_left">
                    <div class="caozuo">
                        @Html.ActionLink("创建", "Add", null, new { id = "lbtnAdd" })
                        <a href="#" onclick="$('#btnDeleteSelected').click()">删除所选</a>
                        <input class="batchAction" id="btnDeleteSelected" name="DeleteSelected" type="submit"
                            value="删除所选" style="display: none" />
                    </div>
                    <div class="seachBox">
                        @Html.HiddenFor(x => x.JobQueryStatus)
                        @Html.TextBoxFor(x => x.Keyword, new { @class = "search_input", id = "txtKeyword" })
                        <input id="btnSearch" class="searchBtns" value="过滤" style="text-align: center; " />
                    </div>
                    <div class="gjss">
                        <a href="#">高级搜索</a></div>
                    <div class="cl">
                    </div>
                </div>
                <div class="search_right">
                    @Html.Pager(Model.PagingFilteringContext).QueryParam("pagenumber")
                </div>
                <div class="cr">
                </div>
            </div>
            <table cellpadding="0" cellspacing="0" width="100%" id="tblJobList">
                <thead>
                    <tr>
                        <th>
                            <input class="chkAll" type="checkbox" />
                        </th>
                        <th>
                            &nbsp;
                        </th>
                        <th id="colSortOrder">
                            排序
                        </th>
                        <th id="colSortOrderInput">
                            排序
                        </th>
                        <th>
                            职位名称
                        </th>
                        <th>
                            招聘部门
                        </th>
                        <th>
                            工作地点
                        </th>
                        <th id="colDateCreated">
                            新建日期
                        </th>
                        <th id="colStartDate">
                            发布日期
                        </th>
                        <th id="colReposted">
                            刷新时间
                        </th>
                        <th id="colEndDate">
                            截止时间
                        </th>
                        <th id="colDateDeleted">
                            删除时间
                        </th>
                        <th>
                            收到简历
                        </th>
                        <th>
                            操作
                        </th>
                    </tr>
                </thead>
                <tbody id="tbodyJobList">
                    @Html.EditorFor(x => x.JobPositionList)
                </tbody>
            </table>
            <div class="pagelists">
                <div class="pagelists_btns">
                    <div class="allCheck">
                        <input class="chkAll" type="checkbox" /><span>全选</span>
                    </div>
                    &nbsp;
                    <input class="batchAction" id="btnPauseSelected" name="PauseSelected" type="submit"
                        value="暂停所选" />
                    <input class="batchAction" id="btnRefreshSelected" name="RefreshSelected" type="submit"
                        value="刷新所选" />
                    <input class="batchAction" id="btnPostSelected" name="PostSelected" type="submit"
                        value="上线所选" />
                    <input class="batchAction" id="btnRestoreSelected" name="RestoreSelected" type="submit"
                        value="还原所选" />
                </div>
                <div class="search_right">
                    @Html.Pager(Model.PagingFilteringContext).QueryParam("pagenumber")
                </div>
                <div class="cr">
                </div>
            </div>
        }
        <ul id="menuItem" style="width: 70px; position: absolute; z-index: 100">
            <li><a href="#" id="lbtnPreview">预览</a></li>
            <li><a href="#" onclick="$('#btnRefresh').click()" id="lbtnRefresh">刷新</a></li>
            <li><a href="#" onclick="$('#btnPause').click()" id="lbtnPause">暂停</a></li>
            <li><a href="#" onclick="$('#btnPost').click()" id="lbtnPost">上线</a></li>
            <li><a href="#" onclick="$('#btnDelete').click()" id="lbtnDelete">删除</a></li>
        </ul>
        @*@using (Html.BeginForm(new { jobQueryStatus = Model.JobQueryStatus, keyword = Model.Keyword }))*@
        @using (Html.BeginForm())
        {
            <input type="hidden" name="id" id="hidSubmitId" />
            <input type="hidden" name="keyword" id="hidSubmitKeyword" />
@*<input type="submit" value="预览" id="btnPreview" name="Preview"/>*@
            <input class="action" type="submit" value="刷新" id="btnRefresh" name="Refresh" style="display: none" />
            <input class="action" type="submit" value="暂停" id="btnPause" name="Pause" style="display: none" />
            <input class="action" type="submit" value="上线" id="btnPost" name="Post" style="display: none" />
            <input class="action" type="submit" value="删除" id="btnDelete" name="Delete" style="display: none" />
            <input class="action" type="submit" value="还原" id="btnRestore" name="Restore" style="display: none" />
        }
    </div>
</div>
